package com.example.demo.controller;

import com.aliyun.oss.OSS;
import com.example.demo.configuration.AliyunOssConfig;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.BadgeManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("badgeManage")
@Api(value = "徽章管理接口", tags = {"徽章管理接口"})
@RestController
@Slf4j
public class BadgeManageController {
    @Autowired
    private BadgeManageService service;

    private OSS ossClient;

    @ResponseBody
    @ApiOperation(value = "获取屬性列表接口")
    @RequestMapping(value = "fetchTabs")
    public Result fetchTabs() {
        List<BadgeView> tabs = service.getTabsList();
        return Result.ok("成功", tabs);
    }

    @ResponseBody
    @ApiOperation(value = "获取徽章列表接口")
    @RequestMapping(value = "getBadgeList", method = {RequestMethod.POST})
    public Result<PageVo<BadgeView>> getList(@RequestBody BadgeView badge) {
        PageVo<BadgeView> list = service.getBadgeList(badge);
        return Result.ok(list);
    }

    //成長徽章
    @ResponseBody
    @ApiOperation(value = "获取成長徽章列表接口")
    @RequestMapping(value = "getGrowthBadgeList", method = {RequestMethod.POST})
    public Result<PageVo<BadgeView>> getGrowthList(@RequestBody PageBo pageBo) {
        PageVo<BadgeView> list = service.getGrowthBadgeList(pageBo);
        return Result.ok(list);
    }

    //成就徽章
    @ResponseBody
    @ApiOperation(value = "获取成就徽章列表接口")
    @RequestMapping(value = "getAchievementBadgeList", method = {RequestMethod.POST})
    public Result<PageVo<BadgeView>> getAchievementBadgeList(@RequestBody PageBo pageBo) {
        PageVo<BadgeView> list = service.getAchievementBadgeList(pageBo);
        return Result.ok(list);
    }

    //特殊徽章
    @ResponseBody
    @ApiOperation(value = "获取特殊徽章列表接口")
    @RequestMapping(value = "getSpecialBadgeList", method = {RequestMethod.POST})
    public Result<PageVo<BadgeView>> getSpecialBadgeList(@RequestBody PageBo pageBo) {
        PageVo<BadgeView> list = service.getSpecialBadgeList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索列表接口")
    @RequestMapping(value = "searchBadgeList", method = {RequestMethod.POST})
    public Result<PageVo<BadgeView>> getListSearch(@RequestBody BadgeView request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<BadgeView> logs = service.searchBadgeList(request,pageBo);
        return Result.ok(logs);
    }

    @ResponseBody
    @RequestMapping(value = "addBadge", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("徽章新增接口")
    public Result<String> addBadge(@RequestBody BadgeView request) {
        return service.addBadge(request);
    }

    @ResponseBody
    @ApiOperation("删除徽章接口")
    @RequestMapping(value = "deleteBadge", method = {RequestMethod.POST})
    public Result<String> delete(@RequestBody BadgeView request) {
        return service.deleteBadge(request);
    }

    @ResponseBody
    @ApiOperation("批量删除徽章接口")
    @RequestMapping(value ="batchDeleteBadge", method = {RequestMethod.POST})
    public Result<String> deleteList(@RequestBody List<Badge> badgeList) {
        service.batchDeleteBadge(badgeList);
        return Result.ok("删除成功");
    }

    @ResponseBody
    @ApiOperation("修改徽章接口")
    @RequestMapping(value = "updateBadgeList", method = {RequestMethod.POST})
    public Result<String> updateBadgeList(@RequestBody BadgeView request) {
        return service.updateBadgeList(request);
    }

    @ResponseBody
    @ApiOperation("徽章圖片显示接口")
    @RequestMapping(value = "findBadgePic/{badgeId}")
    public Result<String> findCommodityPic(@PathVariable("badgeId")int badgeId) {
        Result<String> message = new Result<>();
        if (badgeId != 0) {
            Badge result = service.getById(badgeId);
            String imageUrl = result.getImageUrl();
            if(imageUrl != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + imageUrl);
                return message;
            }
            else{
                return null;
            }
        }
        return null;
    }

    @ApiOperation(value = "徽章圖片上传")
    @PostMapping("/uploadBadge")
    public Result uploadCommodity(@RequestParam("badgeId") int badgeId, @RequestParam("file") MultipartFile file) throws IOException {

        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "badge/" + UUID.randomUUID().toString().substring(0,24)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName

        Badge currentBadge = service.selectBadgeById(badgeId);
        currentBadge.setImageUrl(fileName);
        service.placeBadgePic(currentBadge);
        ossClient.shutdown();

        String result="http://seedling-classmate.oss-cn-beijing.aliyuncs.com/"+fileName;
        return Result.ok("上传成功",result);

    }


    @ResponseBody
    @ApiOperation(value = "获取徽章列表接口")
    @RequestMapping(value = "getBadgeLog", method = {RequestMethod.POST})
    public Result<PageVo<StudentBadgeView>> getBadgeLog(@RequestBody PageBo pageBo) {
        PageVo<StudentBadgeView> list = service.getBadgeLog(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索列表接口")
    @RequestMapping(value = "searchBadgeLog", method = {RequestMethod.POST})
    public Result<PageVo<StudentBadgeView>> searchBadgeLog(@RequestBody StudentBadgeView request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<StudentBadgeView> logs = service.searchBadgeLog(request,pageBo);
        return Result.ok(logs);
    }


}
